package controllers;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import common.gson.GBuilder;
import common.gson.GsonUtils;
import play.exceptions.UnexpectedException;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.results.Result;

public class _RenderJson  extends Result {
	
	String json;
	
	public _RenderJson(Object o) {
 
		try
		{
			json = GsonUtils.gson.toJson( o ) ;
		}
		catch( Throwable e )
		{
			e.printStackTrace();
		}
	}
	
	@Override
	public void apply(Request request, Response response) {
		  try {
	            String encoding = getEncoding();
	            setContentTypeIfNotSet(response, "application/json; charset=" + encoding);
	            response.out.write(json.getBytes(encoding));
	        } catch (Exception e) {
	            throw new UnexpectedException(e);
	        }
	}
	
}
